package crossbreed.tests.models;

import java.util.ArrayList;
import junit.framework.Assert;
import org.junit.Before;
import org.junit.Test;
import crossbreed.diff.DiffItem;
import crossbreed.models.ComparisonModel.ComparisonLine;
import crossbreed.models.ComparisonModel.ComparisonModel;
import crossbreed.models.ComparisonModel.LineType;
import crossbreed.tests.mocks.utilities.MockDiff;
import crossbreed.tests.mocks.utilities.MockErrorLogger;
import crossbreed.tests.mocks.utilities.MockFileReader;

public class ComparisonModelTests
{
	private MockDiff mockDiff;
	private MockErrorLogger mockLogger;
	private MockFileReader mockLeftReader;
	private MockFileReader mockRightReader;
	private ComparisonModel model;
	
	@Before
	public void initialize()
	{
		mockDiff = new MockDiff();
		mockLogger = new MockErrorLogger();
		mockLeftReader = new MockFileReader();
		mockRightReader = new MockFileReader();
		model = new ComparisonModel(mockDiff, mockLogger, mockLeftReader, mockRightReader);
	}
	
	@Test
	public void shouldReturnAllSame()
	{
		mockLeftReader.lines.add("A");
		mockLeftReader.lines.add("B");
		mockLeftReader.lines.add("C");	
		model.createDiff();
		
		ArrayList<ComparisonLine> leftLines = model.getLeftLines();
		Assert.assertEquals(LineType.SAME, leftLines.get(0).lineType);
		Assert.assertEquals(LineType.SAME, leftLines.get(1).lineType);
		Assert.assertEquals(LineType.SAME, leftLines.get(2).lineType);
	}
	
	@Test
	public void shouldReturnOneSwapOneInsertAndSame()
	{
		mockLeftReader.lines.add("A");
		mockLeftReader.lines.add("B");
		mockLeftReader.lines.add("C");
		
		DiffItem diffItem = new DiffItem();
		diffItem.deletedA = 1;
		diffItem.insertedB = 2;
		mockDiff.diffItems.add(diffItem);
		
		model.createDiff();
		ArrayList<ComparisonLine> leftLines = model.getLeftLines();
		Assert.assertEquals(LineType.SWAP, leftLines.get(0).lineType);
		Assert.assertEquals(LineType.INSERT, leftLines.get(1).lineType);
		Assert.assertEquals(LineType.SAME, leftLines.get(2).lineType);
	}
	
	@Test
	public void shouldReturnOneSameTwoSwapOneSame()
	{
		mockLeftReader.lines.add("A");
		mockLeftReader.lines.add("B");
		mockLeftReader.lines.add("C");
		mockLeftReader.lines.add("D");
		
		DiffItem diffItem = new DiffItem();
		diffItem.StartA = 1;
		diffItem.StartB = 1;
		diffItem.deletedA = 2;
		diffItem.insertedB = 2;
		mockDiff.diffItems.add(diffItem);
		
		model.createDiff();
		ArrayList<ComparisonLine> leftLines = model.getLeftLines();
		Assert.assertEquals(LineType.SAME, leftLines.get(0).lineType);
		Assert.assertEquals(LineType.SWAP, leftLines.get(1).lineType);
		Assert.assertEquals(LineType.SWAP, leftLines.get(2).lineType);
		Assert.assertEquals(LineType.SAME, leftLines.get(3).lineType);
	}
	
	@Test
	public void shouldReturnOneDeleteAndThreeSame()
	{
		mockLeftReader.lines.add("A");
		mockLeftReader.lines.add("B");
		mockLeftReader.lines.add("C");
		mockLeftReader.lines.add("D");
		
		DiffItem diffItem = new DiffItem();
		diffItem.deletedA = 1;
		mockDiff.diffItems.add(diffItem);
		
		model.createDiff();
		ArrayList<ComparisonLine> leftLines = model.getLeftLines();
		Assert.assertEquals(LineType.DELETE, leftLines.get(0).lineType);
		Assert.assertEquals(LineType.SAME, leftLines.get(1).lineType);
		Assert.assertEquals(LineType.SAME, leftLines.get(2).lineType);
		Assert.assertEquals(LineType.SAME, leftLines.get(3).lineType);
	}
}
